package com.turing.post.cardmanagement.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.capitalaccounts.util.CapitalApplicationException;
import com.turing.post.cardmanagement.util.CardManaRespCode;
import com.turing.post.cardmanagement.util.ProxyIssue;
import com.turing.post.cardmanagement.util.ProxyIssueDao;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * @author zxp
 * @Mar 12, 2013
 */
@Stateless(mappedName = "cardmanagement.impl.ProxyIssueDaoImpl")
@Remote
@Local
public class ProxyIssueDaoImpl implements ProxyIssueDao {

	/**
	 * 实体管理器
	 */
	@PersistenceContext
	protected EntityManager _em;

	@Override
	public void deleteProxyIssue(PCPSWebInput input, String proxyIssue)
			throws PCPSApplicationException {
		if (input == null || PCPSUtil.isEmpty(input.getOperBranchNo())) {
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRMSG_NULLPOINTER,
					CardManaRespCode.RC_ARGS_NULL, "登录用户归属机构为空!");
		}
		if (!PCPSUtil.isEmpty(proxyIssue)) {
			ProxyIssue pi = getProxyIssueByProxyIssueBin(proxyIssue);
			if (pi != null) {
				GeneralDao.remove(_em, pi);
			}
		}
	}

	@Override
	public ProxyIssue getProxyIssueByProxyIssueBin(String proxyIssueBin) {
		return _em.find(ProxyIssue.class, proxyIssueBin);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ProxyIssue> getProxyIssueDropDownList(PCPSWebInput input) {
		if (input == null || PCPSUtil.isEmpty(input.getOperBranchNo())) {
			return null;
		}
		String issueCompanyNo = PCPSUtil.getIssueCompanyNo(input);
		String sql = "select * from ProxyIssue where IssueCompanyNo='"
				+ issueCompanyNo + "' order by proxyIssueBin";
		Query query = _em.createNativeQuery(sql, ProxyIssue.class);
		return query.getResultList();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ProxyIssue> getProxyIssueListByCondition(PCPSWebInput input,
			Integer page) {
		if (input == null || PCPSUtil.isEmpty(input.getOperBranchNo())) {
			return null;
		}
		String issueCompanyNo = PCPSUtil.getIssueCompanyNo(input);
		String sql = "select * from ProxyIssue where IssueCompanyNo='"
				+ issueCompanyNo + "' order by proxyIssueBin";
		Query query = _em.createNativeQuery(sql, ProxyIssue.class);
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		query.setFirstResult(startPosition);
		query.setMaxResults(maxResult);
		return query.getResultList();
	}

	@Override
	public BigInteger getProxyIssueTotalByCondition(PCPSWebInput input) {
		if (input == null || PCPSUtil.isEmpty(input.getOperBranchNo())) {
			return BigInteger.ZERO;
		}
		String issueCompanyNo = PCPSUtil.getIssueCompanyNo(input);
		String sql = "select nvl(count(*),0) from ProxyIssue where IssueCompanyNo='"
				+ issueCompanyNo + "'";
		Query query = _em.createNativeQuery(sql);
		return new BigInteger(query.getSingleResult().toString());
	}

	@Override
	public void insertProxyIssue(PCPSWebInput input, ProxyIssue proxyIssue)
			throws PCPSApplicationException {
		if (input == null || PCPSUtil.isEmpty(input.getOperBranchNo())) {
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRMSG_NULLPOINTER,
					CardManaRespCode.RC_ARGS_NULL, "登录用户归属机构为空!");
		}
		if (PCPSUtil.isEmpty(proxyIssue.getProxyIssueBin())) {
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRMSG_NULLPOINTER,
					CardManaRespCode.RC_ARGS_NULL, "代发卡前缀为空!");
		}
		if (PCPSUtil.isEmpty(proxyIssue.getIssueBinName())) {
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRMSG_NULLPOINTER,
					CardManaRespCode.RC_ARGS_NULL, "代发卡名称为空!");
		}

		String issueCompanyNo = PCPSUtil.getIssueCompanyNo(input);
		proxyIssue.setIssueCompanyNo(issueCompanyNo);
		GeneralDao.insert(_em, proxyIssue);
	}

	@Override
	public void updateProxyIssue(PCPSWebInput input, ProxyIssue proxyIssue)
			throws PCPSApplicationException {
		if (input == null || PCPSUtil.isEmpty(input.getOperBranchNo())) {
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRMSG_NULLPOINTER,
					CardManaRespCode.RC_ARGS_NULL, "登录用户归属机构为空!");
		}
		if (PCPSUtil.isEmpty(proxyIssue.getProxyIssueBin())) {
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRMSG_NULLPOINTER,
					CardManaRespCode.RC_ARGS_NULL, "代发卡前缀为空!");
		}
		if (PCPSUtil.isEmpty(proxyIssue.getIssueBinName())) {
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRMSG_NULLPOINTER,
					CardManaRespCode.RC_ARGS_NULL, "代发卡名称为空!");
		}
		ProxyIssue _proxyIssue = getProxyIssueByProxyIssueBin(proxyIssue
				.getProxyIssueBin());
		if (_proxyIssue == null) {
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRMSG_NULLPOINTER,
					CardManaRespCode.RC_ARGS_NULL, "待修改代发卡不存在!");
		}
		_proxyIssue.setIssueBinName(proxyIssue.getIssueBinName());
		_proxyIssue.setRemark(proxyIssue.getRemark());
		GeneralDao.update(_em, _proxyIssue);
	}
}